package com.gusha.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import java.math.BigDecimal;
import java.math.BigInteger;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gusha.dao.ProjectDao;
import com.gusha.model.Project;
import com.gusha.model.SysArea;
import com.gusha.model.SysClassify;
import com.gusha.model.User;


/**
 * @Created：2018-11-09 14:21:54
 * @Author LightSound的代码生成器
 * @Description:ProjectService服务类
 */

@Service
@Transactional
public class ProjectService {

    @Autowired
    private ProjectDao projectDao;

    public Page<Project> findAll(Pageable pageable, Project project) {
        @SuppressWarnings("serial")
        Page<Project> result = projectDao.findAll(new Specification<Project>() {
            @Override
            public Predicate toPredicate(Root<Project> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                if (project.getId() != null) {
                    list.add(cb.equal(root.get("id").as(BigInteger.class),project.getId()));
                }
                if (project.getUser() != null) {
                    list.add(cb.equal(root.get("user").as(User.class),project.getUser()));
                }
                if (project.getClassify() != null) {
                    list.add(cb.equal(root.get("classify").as(SysClassify.class),project.getClassify()));
                }
                if (StringUtils.isNotBlank(project.getProjectName())) {
                    list.add(cb.like(root.get("projectName").as(String.class),"%"+project.getProjectName()+"%"));
                }
                if (StringUtils.isNotBlank(project.getProjectNo())) {
                    list.add(cb.equal(root.get("projectNo").as(String.class),project.getProjectNo()));
                }
                if (StringUtils.isNotBlank(project.getProjectDesc())) {
                    list.add(cb.equal(root.get("projectDesc").as(String.class),project.getProjectDesc()));
                }
                if (StringUtils.isNotBlank(project.getProjectContent())) {
                    list.add(cb.equal(root.get("projectContent").as(String.class),project.getProjectContent()));
                }
                if (StringUtils.isNotBlank(project.getProjectIcon())) {
                    list.add(cb.equal(root.get("projectIcon").as(String.class),project.getProjectIcon()));
                }
                if (StringUtils.isNotBlank(project.getProjectImages())) {
                    list.add(cb.equal(root.get("projectImages").as(String.class),project.getProjectImages()));
                }
                if (StringUtils.isNotBlank(project.getProjectVideo())) {
                    list.add(cb.equal(root.get("projectVideo").as(String.class),project.getProjectVideo()));
                }
                if (project.getProjectMoney()!=null) {
                    list.add(cb.equal(root.get("projectMoney").as(BigDecimal.class),project.getProjectMoney()));
                }
                if (project.getProjectProvince()!=null) {
                    list.add(cb.equal(root.get("projectProvince").as(SysArea.class),project.getProjectProvince()));
                }
                if (project.getProjectCity()!=null) {
                    list.add(cb.equal(root.get("projectCity").as(SysArea.class),project.getProjectCity()));
                }
                if (project.getProjectDistrict()!=null) {
                    list.add(cb.equal(root.get("projectDistrict").as(SysArea.class),project.getProjectDistrict()));
                }
                if (StringUtils.isNotBlank(project.getProjectAddress())) {
                    list.add(cb.equal(root.get("projectAddress").as(String.class),project.getProjectAddress()));
                }
                if (StringUtils.isNotBlank(project.getSeoKey())) {
                    list.add(cb.equal(root.get("seoKey").as(String.class),project.getSeoKey()));
                }
                if (StringUtils.isNotBlank(project.getSeoTitle())) {
                    list.add(cb.equal(root.get("seoTitle").as(String.class),project.getSeoTitle()));
                }
                if (StringUtils.isNotBlank(project.getSeoDesc())) {
                    list.add(cb.equal(root.get("seoDesc").as(String.class),project.getSeoDesc()));
                }
                if (project.getStartTime() != null) {
                    list.add(cb.equal(root.get("startTime").as(Date.class),project.getStartTime()));
                }
                if (project.getEndTime() != null) {
                    list.add(cb.equal(root.get("endTime").as(Date.class),project.getEndTime()));
                }
                if (project.getProjectDays() != null) {
                    list.add(cb.equal(root.get("projectDays").as(Integer.class),project.getProjectDays()));
                }
                if (project.getIsChecked() != null) {
                    list.add(cb.equal(root.get("isChecked").as(Integer.class),project.getIsChecked()));
                }
                if (project.getCheckTime() != null) {
                    list.add(cb.equal(root.get("checkTime").as(Date.class),project.getCheckTime()));
                }
                if (StringUtils.isNotBlank(project.getCheckor())) {
                    list.add(cb.equal(root.get("checkor").as(String.class),project.getCheckor()));
                }
                if (StringUtils.isNotBlank(project.getCheckRemark())) {
                    list.add(cb.equal(root.get("checkRemark").as(String.class),project.getCheckRemark()));
                }
                if (project.getCreateTime() != null) {
                    list.add(cb.equal(root.get("createTime").as(Date.class),project.getCreateTime()));
                }
                if (project.getUpdateTime() != null) {
                    list.add(cb.equal(root.get("updateTime").as(Date.class),project.getUpdateTime()));
                }
                if (StringUtils.isNotBlank(project.getUpdator())) {
                    list.add(cb.equal(root.get("updator").as(String.class),project.getUpdator()));
                }
                if (project.getIsDelete() != null) {
                    list.add(cb.equal(root.get("isDelete").as(Integer.class),project.getIsDelete()));
                }
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        },pageable);
         return result;
    }

}
